/*
 * @lc app=leetcode.cn id=121 lang=javascript
 *
 * [121] 买卖股票的最佳时机
 */

// @lc code=start
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  if (prices.length <= 1) return 0;

  let min = prices[0];
  let rel = 0;

  for (const value of prices) {
    if (value < min) {
      min = value;
    } else {
      rel = Math.max(rel, rel - min);
    }
  }

  return rel;
};
// @lc code=end
